package com.example.callmanagerandroid;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.example.database.Contact;

public class CustomPhoneStateListener extends PhoneStateListener {

	Context context;
	private AudioManager amanager;
	Intent i1;
	Thread t;

	public CustomPhoneStateListener(Context context) {
		super();
		this.context = context;
		i1 = new Intent(context, phoneRing.class);
		i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

	}

	@Override
	public void onCallStateChanged(int state, String incomingNumber) {
		super.onCallStateChanged(state, incomingNumber);

		switch (state) {
		case TelephonyManager.CALL_STATE_IDLE:
			// Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG)
			// .show();

			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:

			// Toast.makeText(context, "Phone state Off hook",
			// Toast.LENGTH_LONG)
			// .show();

			break;
		case TelephonyManager.CALL_STATE_RINGING:
			Log.w("","receiver");
			sendDataToServer(incomingNumber);
			Contact contact = new Contact(context);
			Cursor c = contact.SelectName(incomingNumber);
			String id = " ";
			String ten = " ";
			String dt = " ";
			String dc = " ";
			String gc = " ";
			if (c != null)
				if (c.moveToFirst()) {

					id = c.getString(c.getColumnIndexOrThrow("Ma"));
					ten = c.getString(c.getColumnIndexOrThrow("Ten"));
					dt = c.getString(c.getColumnIndexOrThrow("DienThoai"));
					dc = c.getString(c.getColumnIndexOrThrow("DiaChi"));
					gc = c.getString(c.getColumnIndexOrThrow("TrangThai"));
				}
			c.close();
			contact.close();
			i1.putExtra("number", incomingNumber);
			i1.putExtra("id", id);
			i1.putExtra("ten", ten);
			i1.putExtra("dt", dt);
			i1.putExtra("dc", dc);
			i1.putExtra("gc", gc);
			i1.putExtra("flag", 1);
			try {
				Thread.sleep(500);
				if(getShare())
					context.startActivity(i1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			
			
		default:
			break;
		}
	}
	
	private Boolean getShare(){
		SharedPreferences sharedpreferences = context.getSharedPreferences(MainActivity.MyPREFERENCES, context.MODE_PRIVATE);
		Boolean isShow = sharedpreferences.getBoolean("share", true);
		return isShow;
	}

	public void sendDataToServer(String number) {
		Contact contact = new Contact(context);
		Cursor c = contact.selectUser();
		if (c != null)
			if (c.moveToFirst()) {
				String line = c.getString(c.getColumnIndexOrThrow("Line"));
				String urls = c.getString(c.getColumnIndexOrThrow("url"));
				final String[] urlss = { urls, number, line };
				t = new Thread(new Runnable() {

					@Override
					public void run() {
						HttpClient client = new DefaultHttpClient();
						String url = urlss[0] + "mCallerID=" + urlss[1]
								+ "&mLine=" + urlss[2];
						HttpGet request = new HttpGet(url);
						ResponseHandler<String> responseHandler = new BasicResponseHandler();
						String response_str = "******";
						try {
							response_str = client.execute(request,
									responseHandler);
						} catch (ClientProtocolException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}

					}
				});
				t.start();
				c.close();
				contact.close();
			}
	}
}
